#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Linklist
{
    char* name;
    int height;
    int weight;
    struct Linklist *next;
}Node;

void set_Node(Node **s1,Node **s2,Node **s3,Node **s4,Node **s5,Node **s6)
{
    *s1 = (Node*)malloc(sizeof(Node));
    *s2 = (Node*)malloc(sizeof(Node));
    *s3 = (Node*)malloc(sizeof(Node));
    *s4 = (Node*)malloc(sizeof(Node));
    *s5 = (Node*)malloc(sizeof(Node));
    *s6 = (Node*)malloc(sizeof(Node)); 

    (*s1)->name = (char*)malloc(10*sizeof(char));
    (*s2)->name = (char*)malloc(10*sizeof(char));
    (*s3)->name = (char*)malloc(10*sizeof(char));
    (*s4)->name = (char*)malloc(10*sizeof(char));
    (*s5)->name = (char*)malloc(10*sizeof(char));
    (*s6)->name = (char*)malloc(10*sizeof(char));


    strcpy((*s1)->name,"张三");
    (*s1)->height = 168;
    (*s1)->weight = 65;
    (*s1)->next = (*s2);

    strcpy((*s2)->name,"李四");
    (*s2)->height = 172;
    (*s2)->weight = 59;
    (*s2)->next = (*s3);

    strcpy((*s3)->name,"王五");
    (*s3)->height = 158;
    (*s3)->weight = 56;
    (*s3)->next = (*s4);

    strcpy((*s4)->name,"赵六");
    (*s4)->height = 182;
    (*s4)->weight = 73;
    (*s4)->next = (*s5);

    strcpy((*s5)->name,"文七");
    (*s5)->height = 160;
    (*s5)->weight = 63;
    (*s5)->next = (*s6);

    strcpy((*s6)->name,"陈八");
    (*s6)->height = 161;
    (*s6)->weight = 53;
    (*s6)->next = NULL;

}

int get_Node_len(Node* head)
{
    int a = 0;
    while (head)
    {
       head = head->next;
       a++;
    }
    return a;
}

void printf_Node(Node* head)
{
    Node *temp = head;
    while (temp)
    {
        printf("%s的身高是%dcm,体重是%dkg\n",temp->name,temp->height,temp->weight);
        temp = temp->next;
    }
}

Node* Sort_height_Node(Node *head)
{
    Node* head_out = head;
    Node* temp_head = head;
    Node* previous = head;

    int len = get_Node_len(head);
    while (len)
    {
        while (temp_head->next)
        {
            if (temp_head->height < temp_head->next->height)
            {
                if (previous == temp_head)
                {
                    previous = previous->next;
                    temp_head->next = temp_head->next->next;
                    previous->next = temp_head;
                    head_out = previous;
                }          
                else
                {
                    Node* temp_previous = previous;
                    previous = previous->next->next;
                    temp_previous->next = previous;
                    temp_head->next = temp_head->next->next;
                    previous->next = temp_head;                            
                }
            }
            else
            {
                if (previous == temp_head)
                {
                    temp_head = temp_head->next;
                }
                else
                {
                    previous = previous->next;
                    temp_head = temp_head->next;
                }
            }
        }
        temp_head = head_out;
        previous = head_out;
        len--;
    }
    return head_out;
}

Node *Sort_weight_Node(Node* head)
{
    Node* head_out = head;
    Node* temp_head = head;
    Node* previous = head;

    int len = get_Node_len(head);
    while (len)
    {
        while (temp_head->next)
        {
            if (temp_head->weight < temp_head->next->weight)
            {
                if (previous == temp_head)
                {
                    previous = previous->next;
                    temp_head->next = temp_head->next->next;
                    previous->next = temp_head;
                    head_out = previous;
                }          
                else
                {
                    Node* temp_previous = previous;
                    previous = previous->next->next;
                    temp_previous->next = previous;
                    temp_head->next = temp_head->next->next;
                    previous->next = temp_head;                            
                }
            }
            else
            {
                if (previous == temp_head)
                {
                    temp_head = temp_head->next;
                }
                else
                {
                    previous = previous->next;
                    temp_head = temp_head->next;
                }
            }
        }
        temp_head = head_out;
        previous = head_out;
        len--;
    }
    return head_out;
}

Node* W_add_Node(Node* head,Node* node)
{
    Node* temp_head = head;
    Node* add_node = node;
    Node* previous = head;
    Node* head_out = head;
    while (temp_head)
    {
        if (add_node->weight > temp_head->weight)
        {
            if(previous == temp_head)
            {
                add_node->next = temp_head;
                head_out = add_node;
            }
            else if (temp_head->next == NULL)
            {
                temp_head->next = add_node;
                add_node->next = NULL;
            }
            else
            {
                previous->next = add_node;
                add_node->next = temp_head;
            }
            break;
        }
        else
        {
            if(previous == temp_head)
            {
                temp_head = temp_head->next;
            }
            else
            {
                previous = previous->next;
                temp_head = temp_head->next;
            }
        }
    }
    return head_out;
}

int main(int argc, char const *argv[])
{
    //定义Node类型的变量
    Node *su1,*su2,*su3,*su4,*su5,*su6;

    //分配上面定义的变量对应的空间并且初始化他们的值
    set_Node(&su1,&su2,&su3,&su4,&su5,&su6);

    //通过头节点打印全部信息
    printf("排序前的顺序打印为:\n");
    printf_Node(su1);

    //进行身高从高到低的排序
    Node *H_Sort_new_head = Sort_height_Node(su1);

    printf("按身高排序后的顺序打印为:\n");
    printf_Node(H_Sort_new_head);

    //进行体重从大到小的排序
    Node *W_Sort_new_head = Sort_weight_Node(H_Sort_new_head);

    printf("按体重排序后的顺序打印为:\n");
    printf_Node(W_Sort_new_head);
    
    //现在有一个新人加入,请按照体重加入并进行添加打印全部的信息
    Node* su7 = (Node*)malloc(sizeof(Node));
    su7->name = (char*)malloc(10*sizeof(char));
    strcpy(su7->name,"古九");
    su7->height = 169;
    su7->weight = 80;

    Node *add_new_head = W_add_Node(W_Sort_new_head,su7);
    printf("加入新人后按身高排序后的顺序打印为:\n");
    printf_Node(add_new_head);


    return 0;
}
